package gameUi
{
	import com.greensock.TweenLite;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.setTimeout;

	public class CardChipBox
	{
		private var _cardMC:MovieClip;//牌区域
		private var _chipMC:MovieClip;//筹码区域
		private var _cardBtnText:TextField;//牌按钮文本
		private var _partCardBox:CardChipBox;//分牌区域
		
		private var _chipNum:Number//筹码数
		private var _chipNumArray:Array//筹码下注历史数组（用于复原功能）
		private var _cardArray:Vector.<Card>;//牌对象数组
		private var _cardNumArray:Array;//牌点数数组
		private var _cardNum:int;//牌总点数
		
		private var _tf0:TextFormat;//筹码字体设置
		private var _tf1:TextFormat;//牌字体设置
		
		private var callCardMC:MovieClip;
		private var _arrwMC:MovieClip;//箭头容器
		private var _isBanker:Boolean;//是否是庄家
		private var _index:int;//位置索引
		
		private var _isPart:Boolean;//是否已经分牌
		
		/**
		 * 下注发牌区域管理类
		 **/
		public function CardChipBox(index:int,cardMC:MovieClip,chipMC:MovieClip,arrwMC:MovieClip,cardBtnText:TextField,partCard:CardChipBox=null,isBanker:Boolean = false)
		{
			_index = index;
			_cardMC = cardMC;
			_chipMC = chipMC;
			_arrwMC = arrwMC;
			_partCardBox = partCard;
			_isBanker = isBanker;
			_cardBtnText = cardBtnText;
			_cardArray = new Vector.<Card>();
			_cardNumArray = new Array();
			_chipNumArray = new Array();
			
			callCardMC = new CallBtn();
//			callCardMC.y = -30;
//			callCardMC.x = 10;
			
			_tf0 = new TextFormat();
			_tf0.size = 9;
			_tf1 = new TextFormat();
			_tf1.size = 26;
		}
		
		/**
		 * 播放开始叫牌动画
		 **/
		public function showCallCardArrow():void{
			_arrwMC.addChild(callCardMC);
		}
		
		/**
		 * 停止叫牌动画
		 **/
		public function closeCallCardArrow():void{
			if(_arrwMC.contains(callCardMC)) _arrwMC.removeChild(callCardMC);
		}
		
		/**
		 * 清除去所有筹码
		 **/
		public function clearAllChip():void{
			_chipNumArray = new Array();
			
			BlackJack._gameUi.allChipNum -= _chipNum;
			if(BlackJack._gameUi.allChipNum == 0) BlackJack._gameUi.gameStateChange(0);
			_chipNum = 0;
			while(_chipMC.numChildren>0){
				_chipMC.removeChildAt(0);
			}
			_cardBtnText.visible = true;
		}
		
		/**
		 * 回复上次筹码状态
		 **/
		public function returnChip():void{
			var tempNum:int = _chipNumArray.pop();
			_chipNum -= tempNum;
			BlackJack._gameUi.allChipNum -= tempNum;
			if(BlackJack._gameUi.allChipNum == 0) BlackJack._gameUi.gameStateChange(0);
			changChipNum();
		}
		
		/**
		 * 加注筹码
		 **/
		public function addChip(num:int):void{
			BlackJack._gameUi.allChipNum += num;
//			if(BlackJack._gameUi.allChipNum == 0) BlackJack._gameUi.gameStateChange(0);
			_chipNumArray.push(num);
			
			_chipNum += num;
			trace("_chipNum:",_chipNum);
			changChipNum();
		}
		
		/**
		 * 筹码值变更
		 **/
		public function changChipNum():void{
			while(_chipMC.numChildren>0){
				_chipMC.removeChildAt(0);
			}
			
			if(_chipNum == 0){
				_cardBtnText.visible = true;
				return;
			}
			
			var tempChipNum:int = _chipNum;
			var i:int
			var chip:ChipObject;
			
			var numText:Sprite = pointTextShow(_chipNum);
			
			var tempNum:int = Math.floor(tempChipNum/1000);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(9);
				chip.chipNumber.gotoAndStop(20);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*1000;
			
			tempNum = Math.floor(tempChipNum/500);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(8);
				chip.chipNumber.gotoAndStop(19);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*500;
			
			tempNum = Math.floor(tempChipNum/100);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(7);
				chip.chipNumber.gotoAndStop(16);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*100;
			
			tempNum = Math.floor(tempChipNum/25);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(6);
				chip.chipNumber.gotoAndStop(14);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*25;
			
			tempNum = Math.floor(tempChipNum/10);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(5);
				chip.chipNumber.gotoAndStop(12);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*10;
			
			tempNum = Math.floor(tempChipNum/5);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(4);
				chip.chipNumber.gotoAndStop(11);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*5;
			
			tempNum = Math.floor(tempChipNum/1);
			for(i=0;i<tempNum;i++){
				chip = new ChipObject();
				chip.gotoAndStop(3);
				chip.chipNumber.gotoAndStop(8);
				_chipMC.addChild(chip);
			}
			tempChipNum -= tempNum*1;
			
			
			if(tempChipNum == 0.5){
				chip = new ChipObject();
				chip.gotoAndStop(1);
				chip.chipNumber.gotoAndStop(7);
				_chipMC.addChild(chip);
			}
			
			for(i = 0;i<_chipMC.numChildren;i++){
				_chipMC.getChildAt(i).y = -i*3;
			}
			
			_chipMC.addChild(numText);
			
			SoundManager.instance.play("snd_ChipStack2");
		}
		
		//筹码点数显示
		private function pointTextShow(num:int):Sprite{
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x323232,0.8);
			spr.graphics.drawRect(0,0,20,10);
			var txt:TextField = new TextField();
			txt.textColor = 0xffffff;
			txt.defaultTextFormat = _tf0;
			txt.text = num.toString();
			txt.width = 20;
			txt.y = -3;
			txt.autoSize = TextFieldAutoSize.CENTER;
			spr.addChild(txt);
			spr.x = -10;
			spr.y = 20;
			return spr;
		}
		
		//牌点数显示
		private function pointCardTextShow(num:int,num2:int):Sprite{
//			num++;
//			num2++;
			
			var txt:TextField = new TextField();
			txt.textColor = 0xffffff;
			txt.defaultTextFormat = _tf1;
			if(num2 == 0){//没有A
				txt.text = num.toString();
				_cardNum = num;
			}else{
				if(_cardNumArray.length >2){
					if(num2 != 0 && num2 <= 21){
						txt.text = num2.toString();
						_cardNum = num2;
					}else{
						txt.text = num.toString();
						_cardNum = num;
					}
				}else{
					txt.text = num.toString()+"/"+num2.toString();
					_cardNum = num2;
				}
			}
			if(_cardNum > 21){
//				closeCallCardArrow();
				if(_isBanker){
					setTimeout(bankerBoonCard,2000);
				}else{
					setTimeout(boonCard,2000);
				}
			}
			check21Point();
			if(_isBanker){
				if(_cardNum>=17) cardPointSound();
			}else{
				if(_cardNumArray.length > 2) cardPointSound();
			}
//			if(_index>5 && _index<11 && _cardNumArray.length == 2){
//				BlackJack.cardGameSocketSender.pointPack(_cardNum,_index);
//			}
			BlackJack.cardGameSocketSender.pointPack(_cardNum,_index);
			var spr:Sprite = new Sprite();
			spr.graphics.beginFill(0x323232,0.8);
			spr.graphics.drawRect(0,0,55,35);
			txt.width = 55;
//			txt.y = -3;
			txt.autoSize = TextFieldAutoSize.CENTER;
			spr.addChild(txt);
			spr.x = -55;
			spr.y = 5;
			return spr;
		}
		
		/**
		 * 检测21点
		 **/
		public function check21Point():void{
			if(!_isBanker && _cardNum == 21 && BlackJack._gameUi.isDealingOver){
				setTimeout(send21PointPack,1000);
			}
		}
		
		//发送刚好21点包
		private function send21PointPack():void{
			if(!BlackJack._gameUi.partCardOver) return;
			BlackJack.cardGameSocketSender.point21Pack(_index);
		}
		
		//庄家玩家爆牌
		private function bankerBoonCard():void{
//			setTimeout(closeCard,1000);
		}
		
		//玩家爆牌
		private function boonCard():void{
			trace("玩家爆牌",_cardNum);
			BlackJack.cardGameSocketSender.boonCardPack(_index);
			setTimeout(closeCard,1000);
		}
		
		//天生21点声音
		private function soundBlackJack():void{
			if(_isBanker){
				SoundManager.instance.play("dealer_has_blackjack");
			}else{
				SoundManager.instance.play("soundblackjack");
			}
		}
		
		/**
		 * 发牌 
		 * @param card 牌对象
		 * @param cardNum 牌点数
		 * 
		 */
		public function deal(card:Card,cardNum:int):void{
			trace("发牌",cardNum);
			SoundManager.instance.play("card_moves");
			
			if(_cardMC.numChildren>0) _cardMC.removeChildAt(0);
			
			_cardNumArray.push(cardNum);
			
//			if(_isBanker && _cardNumArray.length == 1 && _cardNumArray[0] == 13){//庄家第一张牌拿A，可买保险
//				BlackJack._gameUi.canIsume();
//			}
			
			var tempNum:int = 0;
			var tempNum1:int = 0;
			for(var i:int=0;i<_cardNumArray.length;i++){
				if(_cardNumArray[i]<=9){
					tempNum += _cardNumArray[i]+1;
				}else if(_cardNumArray[i]>=9 && _cardNumArray[i]<=12){
					tempNum += 10;
				}else if(_cardNumArray[i] == 13){
					tempNum += 1;
//					if(tempNum + 10 < 21) tempNum1 = tempNum + 10;
				}
			}
			for(i=0;i<_cardNumArray.length;i++){
				if(_cardNumArray[i] == 13){
					if(tempNum + 10 <= 21) tempNum1 = tempNum + 10;
				}
			}
			
			_cardMC.addChild(card);
			var tempPoint:Point = _cardMC.globalToLocal(new Point(BlackJack._gameUi.pointCard.x,BlackJack._gameUi.pointCard.y));
//			card.x = tempPoint.x;
//			card.y = tempPoint.y;
			card.x = BlackJack._gameUi.pointCard.x - _cardMC.x;
			card.y = BlackJack._gameUi.pointCard.y - cardMC.y;
			TweenLite.to(card,0.5,{x:_cardArray.length*15+80,y:30,onComplete:dealOver});
			_cardArray.push(card);
			
			var spr:Sprite = pointCardTextShow(tempNum,tempNum1);
			_cardMC.addChildAt(spr,0);
		}
		
		/**
		 * 发分牌 
		 * @param card 牌对象
		 * @param cardNum 牌点数
		 * 
		 */
		public function dealPart(card:Card,cardNum:int):void{
			trace("分牌",cardNum);
			SoundManager.instance.play("card_moves");
			
			if(_cardMC.numChildren>0) _cardMC.removeChildAt(0);
			
			_cardNumArray.push(cardNum);
			
			//			if(_isBanker && _cardNumArray.length == 1 && _cardNumArray[0] == 13){//庄家第一张牌拿A，可买保险
			//				BlackJack._gameUi.canIsume();
			//			}
			
			var tempNum:int = 0;
			var tempNum1:int = 0;
			for(var i:int=0;i<_cardNumArray.length;i++){
				if(_cardNumArray[i]<=9){
					tempNum += _cardNumArray[i]+1;
				}else if(_cardNumArray[i]>=9 && _cardNumArray[i]<=12){
					tempNum += 10;
				}else if(_cardNumArray[i] == 13){
					tempNum += 1;
					//					if(tempNum + 10 < 21) tempNum1 = tempNum + 10;
				}
			}
			for(i=0;i<_cardNumArray.length;i++){
				if(_cardNumArray[i] == 13){
					if(tempNum + 10 <= 21) tempNum1 = tempNum + 10;
				}
			}
			var spr:Sprite = pointCardTextShow(tempNum,tempNum1);
			_cardMC.addChildAt(spr,0);
			
//			var tempPoint:Point = _cardMC.globalToLocal(new Point(card.x,card.y));
//			card.x = tempPoint.x;
//			card.y = tempPoint.y;
			_cardMC.addChild(card);
			TweenLite.to(card,0.5,{x:_cardArray.length*15+80,y:30,onComplete:dealOver});
			_cardArray.push(card);
		}
		
		/**
		 * 分牌
		 **/
		public function packCard():void{
//			_cardMC.getChildAt(_cardMC.numChildren-1).x = _partCardBox.cardMC.x - _cardMC.x;
			_isPart = true;
			if(_cardMC.numChildren>0) _cardMC.removeChildAt(0);
			_cardMC.getChildAt(_cardMC.numChildren-1).y = _cardMC.y -  _partCardBox.cardMC.y;
			var tempCard:Card = _cardArray.pop();
			_partCardBox.dealPart(_cardMC.getChildAt(_cardMC.numChildren-1) as Card,tempCard.cardNum);
			_partCardBox.isPart = true;
			var num:int = 0;
			if(tempCard.cardNum<=9){
				num += tempCard.cardNum+1;
			}else if(tempCard.cardNum>=9 && tempCard.cardNum<=12){
				num += 10;
			}else if(tempCard.cardNum == 13){
				num += 1;
				//					if(tempNum + 10 < 21) tempNum1 = tempNum + 10;
			}
			if(tempCard.cardNum == 13) var num1:int = num+10;
			var spr:Sprite = pointCardTextShow(num,num1);
			_cardMC.addChildAt(spr,0);
			
			_partCardBox.addChip(_chipNum);
			_cardNumArray.pop();
			setTimeout(soundPack,500);
		}
		
		/**
		 * 分牌音效
		 **/
		private function soundPack():void{
			SoundManager.instance.play("split.wav");
		}
		
		/**
		 * 检测是否可以分牌
		 **/
		public function checkPack():Boolean{
			trace("分牌判定：",!_isBanker , _cardNumArray.length == 2 , _cardNumArray[0] == _cardNumArray[1] , !_isPart , _index<5)
			if(!_isBanker && _cardNumArray.length == 2 && _cardNumArray[0] == _cardNumArray[1] && !_isPart && _index<5){//出现对子时
				BlackJack._gameUi.gameStateChange(3);
				setTimeout(playPackSound,1000);
				return true;
			}
			return false;
		}
		
		//播放分牌音效
		private function playPackSound():void{
			SoundManager.instance.play("split_question");
		}
		
		private function dealOver():void{
			SoundManager.instance.play("card_ends");
			BlackJack._gameUi.dealing(false,_isBanker,_cardArray.length,cardNum);
		}
		
		public function get bankerPoint():int{
			return _cardArray[0].cardNum;
		}
		
	//-----------------------------------------------------------
		/**
		 * 收牌(lose)
		 **/
		public function closeCard():void{
			for(var i:int = 0;i<_cardArray.length;i++){
				TweenLite.to(_cardArray[i],0.5,{x:50});
			}
			setTimeout(closeCardAction,500);
		}
		
		private function closeCardAction():void{
			SoundManager.instance.play("card_moves");
			for(var i:int = 0;i<_cardArray.length;i++){
				_cardArray[i].closeBg();
			}
//			_cardArray[_cardArray.length-1].card.gotoAndStop(53);
			var point:Point = new Point();;
//			var point:Point = _cardMC.globalToLocal(new Point(BlackJack._gameUi.pointCards.x,BlackJack._gameUi.pointCards.y));
			point.x = BlackJack._gameUi.pointCards.x - _cardMC.x;
			point.y = BlackJack._gameUi.pointCards.y - _cardMC.y;
			for(i = 0;i<_cardArray.length;i++){
				TweenLite.to(_cardArray[i],0.5,{x:point.x,y:point.y});
			}
//			point = _chipMC.globalToLocal(new Point(BlackJack._gameUi.pointBankerChip.x,BlackJack._gameUi.pointBankerChip.y));
			point.x = BlackJack._gameUi.pointBankerChip.x - _chipMC.x;
			point.y = BlackJack._gameUi.pointBankerChip.y - _chipMC.y;
			for(i = 0;i<_chipMC.numChildren;i++){
				TweenLite.to(_chipMC.getChildAt(i),0.5,{x:point.x,y:point.y});
			}
			setTimeout(closeCardActionOver,500);
		}
		
		private function closeCardActionOver():void{
			while(_cardArray.length>0){
				_cardArray[0].destory();
				_cardArray[0] = null;
//				_cardArray[0].parent.removeChild(_cardArray[0]);
				_cardArray.splice(0,1);
			}
			while(_cardMC.numChildren>0) _cardMC.removeChildAt(0);
			while(_chipMC.numChildren>0){
//				if(_chipMC.getChildAt(0) as Card){
//					(_chipMC.getChildAt(0) as Card).destory();
//				}else{
//				}
				_chipMC.removeChildAt(0);
			}
//			_cardArray = new Vector.<Card>();
			setTimeout(setDealing,2000);
		}
		
		private function setDealing():void{
			BlackJack._gameUi.closeDealing(false);
		}
		
		private function closeChipAction():void{
			BlackJack._gameUi.playerChipCome(_chipNum);
		}
	//-----------------------------------------------------------
		
		//-----------------------------------------------------------
		/**
		 * 收牌(win)
		 **/
		public function closeCard1():void{
			for(var i:int = 0;i<_cardArray.length;i++){
				TweenLite.to(_cardArray[i],0.5,{x:50});
			}
			setTimeout(closeCardAction1,500);
		}
		
		private function closeCardAction1():void{
			for(var i:int = 0;i<_cardArray.length;i++){
				_cardArray[i].closeBg();
			}
			var point:Point = new Point();;
//			= _cardMC.globalToLocal(new Point(BlackJack._gameUi.pointCards.x,BlackJack._gameUi.pointCards.y));
			point.x = BlackJack._gameUi.pointCards.x - _cardMC.x;
			point.y = BlackJack._gameUi.pointCards.y - _cardMC.y;
			for(i = 0;i<_cardArray.length;i++){
				TweenLite.to(_cardArray[i],0.5,{x:point.x,y:point.y});
			}
			point = _chipMC.globalToLocal(new Point(BlackJack._gameUi.pointChipWin.x,BlackJack._gameUi.pointChipWin.y));
			point.x = BlackJack._gameUi.pointChipWin.x - _chipMC.x;
			point.y = BlackJack._gameUi.pointChipWin.y - _chipMC.y;
			for(i = 0;i<_chipMC.numChildren;i++){
				TweenLite.to(_chipMC.getChildAt(i),0.5,{x:point.x,y:point.y});
			}
			setTimeout(closeCardActionOver,500);
			setTimeout(closeChipAction,400);
		}
		//-----------------------------------------------------------
		
		/**
		 * 读牌 返回是否会跳过操作
		 **/
		public function cardPointSound():Boolean{
			if(_cardNum == 21){
//				if(_cardNumArray.length == 2){
//					setTimeout(soundBlackJack,1000);
//					return;
//				}
				if(_cardArray.length == 2 && _isPart == false){
					if(_cardArray[0].cardNum == 13 && _cardArray[1].cardNum >=9 &&  _cardArray[1].cardNum < 13){
						soundBlackJack();
						return true;
					}
					if(_cardArray[1].cardNum == 13 && _cardArray[0].cardNum >=9 &&  _cardArray[0].cardNum < 13){
						soundBlackJack();
						return true;
					}
				}
			}
			SoundManager.instance.playPoints(_cardNum);
			return false;
		}
		
		/**
		 * 初始化
		 **/
		public function initCardChipBox():void{
			_chipNum = 0;
			_cardNum = 0;
			_isPart = false;
			_cardNumArray = new Array();
		}

		/**
		 * 筹码数
		 **/
		public function get chipNum():Number
		{
			return _chipNum;
		}

		public function get cardNum():int
		{
			return _cardNum;
		}

		public function get cardMC():MovieClip
		{
			return _cardMC;
		}

		public function get cardArrayLenth():int
		{
			return _cardArray.length;
		}

		public function set isPart(value:Boolean):void
		{
			_isPart = value;
		}


	}
}